<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-07-24 14:09
 *
 * Version: 2.0
 */

namespace App\Modules\Message\Repositories;


use App\Jobs\MessageQueue;
use App\Lib\Utilities;
use App\Models\Message;
use App\Modules\Message\Validators\MessageValidator;
use App\Modules\System\Repositories\UserStoreRepository;
use Flower\Core\Repository\Eloquent\BaseRepository;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class MessageRepository extends BaseRepository
{
	use DispatchesJobs;

	public function model()
	{
		return Message::class;
	}

	public function validator()
	{
		return MessageValidator::class;
	}


	/**
	 * @param $scene
	 * @param $type
	 * @param $content
	 * @param $noticeFace @消息头像
	 * @param $noticeTitle @消息title
	 * @param $noticeTitleRight @消息副标题
	 * @param $contentLeft @消息left
	 * @param $contentRight @消息right 日期格式是
	 * @return array
	 */
	public function makeMessageData($scene, $type, $noticeFace, $noticeTitle, $noticeTitleRight, $contentLeft, $contentRight, $content = "", $linkId = 0)
	{
		$contentRight = empty($contentRight) ? "" : $contentRight;
		// 理发师消息
		$datumsBarber = [
			"noticeFace"       => $noticeFace,
			"noticeTitle"      => $noticeTitle,
			"noticeTitleRight" => $noticeTitleRight,
			"contentLeft"      => $contentLeft,
			"contentRight"     => strtotime($contentRight) ? Utilities::wxDateFormat($contentRight) : is_int($contentRight) ? Utilities::wxDateFormat($contentRight) : $contentRight
		];
		switch ($type) {
			case  config('constant.message.sub_type.card_handle') :
				$params = [
					'scene'    => $scene,
					'type'     => config('constant.message.type.card'),
					'sub_type' => config('constant.message.sub_type.card_handle'), //  card_handle
					'title'    => config('constant.message.card_handle'), // card_handle
					'content'  => $content,
					'datums'   => json_encode($datumsBarber),
					'link_id'  => $linkId
				];
				break;
			case  config('constant.message.sub_type.new_order') :
				$params = [
					'scene'    => $scene,
					'type'     => config('constant.message.type.order'),
					'sub_type' => config('constant.message.sub_type.new_order'), //  card_handle
					'title'    => config('constant.message.new_order'), // card_handle
					'content'  => $content,
					'datums'   => json_encode($datumsBarber),
					'link_id'  => $linkId
				];
				break;
			case  config('constant.message.sub_type.card_consume') :
				$params = [
					'scene'    => $scene,
					'type'     => config('constant.message.type.card'),
					'sub_type' => config('constant.message.sub_type.card_consume'), //  card_consume
					'title'    => config('constant.message.card_consume'), // card_consume
					'content'  => $content,
					'datums'   => json_encode($datumsBarber),
					'link_id'  => $linkId
				];
				break;
			case  config('constant.message.sub_type.card_recharge') :
				$params = [
					'scene'    => $scene,
					'type'     => config('constant.message.type.card'),
					'sub_type' => config('constant.message.sub_type.card_recharge'), //  card_recharge
					'title'    => config('constant.message.card_recharge'), // card_recharge
					'content'  => $content,
					'datums'   => json_encode($datumsBarber),
					'link_id'  => $linkId
				];
				break;

			case config('constant.message.sub_type.order_cancel') :
				$params = [
					'scene'    => $scene,
					'type'     => config('constant.message.type.order'),
					'sub_type' => config('constant.message.sub_type.order_cancel'), //  card_handle
					'title'    => config('constant.message.order_cancel'), // card_handle
					'content'  => $content,
					'datums'   => json_encode($datumsBarber),
					'link_id'  => $linkId
				];
				break;
			case config('constant.message.sub_type.order_cancel_apply') :
				$params = [
					'scene'    => $scene,
					'type'     => config('constant.message.type.order'),
					'sub_type' => config('constant.message.sub_type.order_cancel_apply'),
					'title'    => $scene == config('constant.message.scene.wx') ? config('constant.message.barber_cancel_apply') : config('constant.message.member_cancel_apply'), //  card_handle
					'content'  => $content,
					'datums'   => json_encode($datumsBarber),
					'link_id'  => $linkId
				];
				break;

			case config('constant.message.sub_type.destroy_cancel_apply') :
				$params = [
					'scene'    => $scene,
					'type'     => config('constant.message.type.order'),
					'sub_type' => config('constant.message.sub_type.refuse_cancel_apply'),
					'title'    => $scene == config('constant.message.scene.wx') ? config('constant.message.barber_destroy_cancel_apply') : config('constant.message.member_destroy_cancel_apply'), //  card_handle
					'content'  => $content,
					'datums'   => json_encode($datumsBarber),
					'link_id'  => $linkId
				];
				break;
//				refuse_cancel_apply

			case config('constant.message.sub_type.refuse_cancel_apply') :
				$params = [
					'scene'    => $scene,
					'type'     => config('constant.message.type.order'),
					'sub_type' => config('constant.message.sub_type.refuse_cancel_apply'),
					'title'    => $scene == config('constant.message.scene.wx') ? config('constant.message.barber_refuse_cancel_apply') : config('constant.message.member_refuse_cancel_apply'), //  card_handle
					'content'  => $content,
					'datums'   => json_encode($datumsBarber),
					'link_id'  => $linkId
				];
				break;


			case config('constant.message.sub_type.order_finish') :
				$params = [
					'scene'    => $scene,
					'type'     => config('constant.message.type.order'),
					'sub_type' => config('constant.message.sub_type.order_finish'), //  card_handle
					'title'    => config('constant.message.order_finish'), // card_handle
					'content'  => $content,
					'datums'   => json_encode($datumsBarber),
					'link_id'  => $linkId
				];
				break;
			case  config('constant.message.sub_type.barber_schedule') :
				$params = [
					'scene'    => $scene,
					'type'     => config('constant.message.type.other'),
					'sub_type' => config('constant.message.sub_type.barber_schedule'), //  card_handle
					'title'    => config('constant.message.barber_schedule'), // card_handle
					'content'  => $content,
					'datums'   => json_encode($datumsBarber),
					'link_id'  => $linkId
				];
				break;
		}
		return $params;
	}

	/**
	 * 新增消息内容
	 * @param $userId
	 * @param $params
	 * @return array
	 */
	public function addMessage($userId, $params, $store_id = 0)
	{
		DB::beginTransaction();
		$message = $this->firstOrCreate($params);
		$messageLog = [
			'message_id' => $message->id,
			'scene'      => $message->scene,
			'recipient'  => $userId,
			'store_id'   => $store_id
		];

		$res = (new MessageLogRepository())->create($messageLog);
		is_object($res) ? DB::commit() : DB::rollBack();
		$res->message;
		$store = $res->store;
		$res = $res->toArray();
		if (isset($res['store'])) {
			unset($res['store']);
			$res['store'] = [
				'id'   => $store->id,
				'name' => $store->name
			];
		}
		if (isset($res['message']['datums'])) unset($res['message']['datums']);
		if ($res['message']['sub_type'] == 6) {
			$res['sound'] = 'audio.wav';
		}
		return $res;
	}


	/**
	 * 给店铺角色发消息
	 * @param $roleType
	 * @param $storeId
	 * @param $messageInfo
	 * @param $userId
	 * @return bool
	 */
	public function sendBossMessage($roleType, $storeId, $messageInfo, $userId=0)
	{
		$bossList = (new UserStoreRepository())->getUserInfoByRoleAndStoreId($roleType, $storeId);
		foreach ($bossList as $v) {
			$message = $this->addMessage($v->uid, $messageInfo, $storeId);
			if ($userId != $v->uid) {
				$this->dispatch(new MessageQueue($v->uid, $message));
			}
		}
		return true;
	}

	/**
	 * 给具体的某个理发师发消息
	 * @param $storeId
	 * @param $barberId
	 * @param $messageInfo
	 * @return bool
	 */
	public function sendBarberMessage($storeId, $barberId, $messageInfo)
	{
		$message = $this->addMessage($barberId, $messageInfo, $storeId);
		$this->dispatch(new MessageQueue($barberId, $message));
		return true;
	}

}